package com.controller;

import com.model.Result;
import com.model.Section;
import com.service.SectionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31.
 */
@Controller
public class SectionController {
    @Resource
    private SectionService sectionService;

    @RequestMapping("getSectionList.do")
    public String getSectionList(@RequestParam(value = "name",required = false)String name,
                                 @RequestParam(value = "pid",required = false)Integer pid,
                                 HttpServletRequest request){
        List<Section> sections = sectionService.getSectionList(new Section(name,pid));
        for (Section section:sections) {
            Integer id = section.getPid();
            String pname = null;
            if(id==0){
                pname = "无";
            }
            else {
                pname = sectionService.getSectionNameById(new Section(id));
            }
            section.setPname(pname);
        }
        List<Section> list = sectionService.getAllSections();
        request.setAttribute("sections",sections);
        request.setAttribute("name",name);
        request.setAttribute("pid",pid);
        request.setAttribute("list",list);
        return "section";
    }

    @RequestMapping("insertSection.do")
    public String insertSection(Section section, HttpSession session, HttpServletRequest request){
        String manager = (String) session.getAttribute("sessionUsername");
        if(manager == null){
            return "login";
        }
        String creator = manager;
        Date date = new Date();
        Long createTime = date.getTime()/1000;
        section.setCreatetime(createTime);
        section.setCreator(creator);
        int result = sectionService.insertSection(section);
        if(result==0){
            request.setAttribute("error","添加失败");
        }
        return getSectionList(null,null,request);
    }

    @RequestMapping("modifySection.do")
    public String modifySection(Section section, HttpServletRequest request){
        int result = sectionService.modifySection(section);
        if(result==0){
            request.setAttribute("error","修改失败");
        }
        return getSectionList(null,null,request);
    }

    @RequestMapping("changeSectionStatus.do")
    public String changeSectionStatus(@RequestParam("id")Integer id,
                                   @RequestParam("status")Integer status,
                                   HttpServletRequest request){
        int result = sectionService.changeStatus(new Section(id,status));
        if(result==0){
            request.setAttribute("error","修改失败");
        }
        return getSectionList(null,null,request);
    }

    @RequestMapping("deleteSection.do")
    public String deleteSection(@RequestParam("id")Integer id,
                             HttpServletRequest request){
        int result = sectionService.deleteSection(new Section(id));
        if(result==0){
            request.setAttribute("error","删除失败");
        }
        return getSectionList(null,null,request);
    }

    @RequestMapping("checkSectionNameExist")
    @ResponseBody
    public Result checkSectionNameExist(@RequestParam("name")String name){
        int count = sectionService.checkExist(new Section(name));
        return new Result(count,null,null);
    }
}
